package cn.hn.tx.rebot.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hn.comm.Tools;

public class TaskKit {
	

	/**
	 * 是否到执行
	 * @param lastRunTime 最后一次执行时间
	 * @param quartz 执行表达式
	 * @return
	 */
	public boolean TimeToGo(String lastRunTime,String quartz)
	{

		//最后一次执行时间细分
		int lastYear;
		int lastMonth;
		int lastDate;
		int lastHourOfDay;
		int lastMinute;
		int lastSecond;		
		
		//当前的时间细分
		int curYear;
		int curMonth;
		int curDate;
		int curHourOfDay;
		int curMinute;
		int curSecond;
		
		Calendar c = Calendar.getInstance();
		c.setTime(Tools.getCurDate(null));
		curYear=c.get(Calendar.YEAR); 
		curMonth=c.get(Calendar.MONTH); 
		curDate=c.get(Calendar.DATE); 
		curHourOfDay=c.get(Calendar.HOUR_OF_DAY); 
		curMinute=c.get(Calendar.MINUTE); 
		curSecond=c.get(Calendar.SECOND); 
		
		
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date sDate = sDateFormat.parse(lastRunTime);
			c=null;
			c = Calendar.getInstance();
			c.setTime(sDate);
			lastYear=c.get(Calendar.YEAR); 
			lastMonth=c.get(Calendar.MONTH); 
			lastDate=c.get(Calendar.DATE); 
			lastHourOfDay=c.get(Calendar.HOUR_OF_DAY); 
			lastMinute=c.get(Calendar.MINUTE); 
			lastSecond=c.get(Calendar.SECOND); 
//			System.out.println(c.get(Calendar.AM_PM)); 
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//解析时间
		String[] arrQuartz=quartz.split(" ");
		//目标时间
		List<Map<Integer,String>> listTargetTime=new ArrayList<Map<Integer,String>>();
		for(int i=0;i<arrQuartz.length;i++)
		{
			//如果不是新，那么就是目标时间
			if(!arrQuartz[i].equals("*"))
			{
				Map map=new HashMap<Integer, String>();
				map.put("index", i);
				map.put("exp", arrQuartz[i]);
				listTargetTime.add(map);
			}
		}
		
		//判断是否运行
		for(int i=listTargetTime.size()-1;i>0;i--)
		{
			Map param=listTargetTime.get(i);
			String index=(String)param.get("index");
			String exp=(String)param.get("exp");
			
			System.out.println("-------------------------------------------");
			System.out.println(index);
			System.out.println(exp);
			
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		TaskKit tk=new TaskKit();
		tk.TimeToGo("2011-05-27 18:24:36", "0 0/5 * * * * *");
	}
	
}
